home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 1.iso / ARGONET / PD / MATHS / RLAB / RLAB125.ZIP / !RLaB / help_ai / eval < prev    next >
Text File  |  1994-07-17  |  2KB  |  98 lines

  1. eval:
  2.  
  3. Syntax:    eval ( S )
  4.  
  5. Description:
  6.  
  7.     The eval function evaluates the statement contained in the
  8.     string argument S. eval returns the result of the statement in
  9.     S. eval can be used within functions and can distinguish local
  10.     and argument variables from global.
  11.  
  12.     Before we go any further, we should note that eval is not
  13.     really a necessary part of RLaB. Users should defintely not
  14.     use it a a crutch as with some other matrix programming
  15.     languages. The RLaB concept of variables, and the list class
  16.     are more efficient ways of dealing with function evaluations
  17.     and variable variable names than eval.
  18.  
  19.     Examples:
  20.  
  21.     > // Evaluate a simple string.
  22.     > // Demonstrate the ability to work with function
  23.     > // arguments.
  24.     >
  25.     > x=function(s,a){return eval(s);}
  26.         <user-function>
  27.     > str = "yy = 2 + x(\"2*a\", 3.5)"
  28.      str =
  29.     yy = 2 + x("2*a", 3.5)
  30.     > z = eval(str)
  31.      z =
  32.             9
  33.     > whos();
  34.         Name            Class    Type    Size        NBytes
  35.         eps                num    real    1    1    16
  36.         pi                 num    real    1    1    16
  37.         str                string    string    1    1    22
  38.         yy                 num    real    1    1    16
  39.         z                  num    real    1    1    16
  40.     Total MBytes = 0.129062
  41.  
  42.     > // First create a function that will eval a matrix.
  43.     >
  44.     > evalm = function ( m )
  45.     > {
  46.     >   local (mnew, i)
  47.     >    
  48.     >   mnew = zeros (size (m));
  49.     >    for (i in 1:m.n)
  50.     >   {
  51.     >     mnew[i] = eval (m[i]);
  52.     >   }
  53.     >    
  54.     >   return mnew;
  55.     > };
  56.     >
  57.     > // Then create a string matrix...
  58.     >
  59.     > mstr = ["x + 1",    "x + sqrt(x)" ;
  60.     >         "cos(2*x)", "sin(sqrt(x))" ]
  61.  
  62.     > x = 2
  63.      x =
  64.             2
  65.     >
  66.     > m = evalm(mstr)
  67.      m =
  68.             3       3.41  
  69.        -0.654      0.988  
  70.     >
  71.     > // Define a second function that does eval twice
  72.     > 
  73.     > eval2m = function ( m )
  74.     > {
  75.     >   local (mnew, i)
  76.     > 
  77.     >   mnew = zeros (size (m));
  78.     >   for (i in 1:m.n)
  79.     >   {
  80.     >     mnew[i] = eval (eval (m[i]));
  81.     >   }
  82.     > 
  83.     >   return mnew;
  84.     > };
  85.     > 
  86.     > mstr = [ "E1", "E2" ;
  87.     >          "E2", "E3" ]
  88.      mstr =
  89.     E1  E2  
  90.     E2  E3  
  91.     > E1 = "cos(2*x) + 3";
  92.     > E2 = "tan(x)";
  93.     > E3 = "exp(x)";
  94.     > m = eval2m(mstr)
  95.      m =
  96.          2.35      -2.19  
  97.         -2.19       7.39  
  98.